package com.zznode.client.wlanpolling.test;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import android.widget.Toast;

import com.zznode.client.wlanpolling.model.TestResultConstants;
import com.zznode.client.wlanpolling.model.WLANPollManager;
import com.zznode.client.wlanpolling.utils.PingUtils;



public class FTPDownloadActivity extends FTPRateChartActivity {
	
	@Override
	protected String getLabel() {
		return "FTP下载测试：";
	}
	
	@Override
	protected boolean isValidForTesting() {
		return true;
	}

	private String replaceStr(String str1, String str2) {
		return str1.replace(str2, "");
	}
	
	@Override
	protected void doSave() {
		String apMac = PingUtils.clearMacAdress(application.getContext().getCurrentWifi().getBSSID());
		
		Map<String, Object> values = new HashMap<String, Object>();
		
		values.put(TestResultConstants.DOWNLOADDATASIZE, replaceStr(tv_allsize.getText().toString(), "KB"));
		values.put(TestResultConstants.DOWNLOADSPEED, replaceStr(tv_avgSpeed.getText().toString(), "KB/s"));
		values.put(TestResultConstants.SSID, application.getContext().getCurrentWifi().getSSID());
		
		WLANPollManager.getWLANPollManager().updateTestResult(application.getApplicationContext(), 
				apMac,
				values);
		
		Toast.makeText(this, "成功保存", Toast.LENGTH_SHORT).show();
	}

	protected void doFTPOperation(FTPClient client) throws Exception{
		int totalSize = 0;
//		long totalTime = 0;
		long currentTime = 0, lastTime = 0, lastSize = 0;
		long beginTime = 0;
		int count = 0;
		
		String[] dirs = dir.split("/");
		if(dirs != null && dirs.length > 0){
			for(int i = 0; i < dirs.length; i++){
				client.changeWorkingDirectory(dirs[i]);
			}
		}
		
		FTPFile[] files = client.listFiles(fileName);
		if(files != null && files.length == 1){
			allSize = files[0].getSize();
		}
		
		InputStream is = new BufferedInputStream(
				client.retrieveFileStream(fileName),
				client.getBufferSize());
		
		beginTime = System.currentTimeMillis();
		byte[] buffer = new byte[client.getBufferSize()];
		int val = 0;
		
		while (!stopped && (val = is.read(buffer)) != -1) {
			totalSize += val;
			
			long systemTime = System.currentTimeMillis();
			if(systemTime - currentTime > 1000) {
				count++;
				lastTime = currentTime;
				currentTime = systemTime;
				//totalTime = currentTime - beginTime;
				int speed = (int)((totalSize - lastSize) /((systemTime - lastTime)/1000));
				if (systemTime - beginTime > 1000) {
					int avgSpeed = (int)(totalSize / ((systemTime - beginTime) / 1000));
					currentSpeed = speed/1024;
					averageSpeed = avgSpeed/1024;
					updateSpeed();
				}
				category.clear();
				double realValue = (double)speed / (double)1024;
				category.add("", dashboardValue(realValue));
				dataset.getSeriesAt(0).add(count, speed/1024.0);
				lastSize = totalSize;
				updateChart();
				completedSize = totalSize;
				updateSummary();
			}
		}
		is.close();
		completedSize = totalSize;
		
	}
}
